<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是通信协议——I2C通信相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="通信协议-03-I2C通信">
<meta property="og:url" content="https://sumumm.github.io/post/1c55a2ac.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是通信协议——I2C通信相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903122402843.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20230502083209035.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903122435393.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903122708365.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903124720178.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903124206035.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20230507104655753.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903151323709.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903151831570.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903153506965.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20230502090201529.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903160151362.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903162111744.png">
<meta property="article:published_time" content="2022-08-30T09:31:13.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.060Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="通信协议">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903122402843.png">


<link rel="canonical" href="https://sumumm.github.io/post/1c55a2ac.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/1c55a2ac.html","path":"post/1c55a2ac.html","title":"通信协议-03-I2C通信"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>通信协议-03-I2C通信 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81I2C%E7%AE%80%E4%BB%8B"><span class="nav-text">一、I2C简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-I2C%E4%BB%8B%E7%BB%8D"><span class="nav-text">1. I2C介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%A8%A1%E6%8B%9FI2C%E4%B8%8E%E7%A1%AC%E4%BB%B6I2C"><span class="nav-text">2. 模拟I2C与硬件I2C</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-I2C-%E6%80%BB%E7%BA%BF%E7%89%B9%E7%82%B9"><span class="nav-text">3. I2C 总线特点</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%9F%BA%E6%9C%AC%E5%8E%9F%E7%90%86%EF%BC%9F"><span class="nav-text">二、基本原理？</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%B8%80%E4%B8%AA%E5%AE%9E%E4%BE%8B"><span class="nav-text">1. 一个实例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AE%9E%E4%BE%8B%E6%80%BB%E7%BB%93"><span class="nav-text">2. 实例总结</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81I2C-%E5%8D%8F%E8%AE%AE%E7%9B%B8%E5%85%B3%E6%9C%AF%E8%AF%AD"><span class="nav-text">三、I2C 协议相关术语</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%9B%B8%E5%85%B3%E5%AE%8F%E5%AE%9A%E4%B9%89"><span class="nav-text">1. 相关宏定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%A9%BA%E9%97%B2%E7%8A%B6%E6%80%81"><span class="nav-text">2. 空闲状态</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E8%B5%B7%E5%A7%8B%E4%BF%A1%E5%8F%B7%E5%92%8C%E5%81%9C%E6%AD%A2%E4%BF%A1%E5%8F%B7"><span class="nav-text">2. 起始信号和停止信号</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E7%9B%B8%E5%85%B3%E6%A6%82%E5%BF%B5"><span class="nav-text">2.1相关概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E8%BD%AF%E4%BB%B6%E6%A8%A1%E6%8B%9F"><span class="nav-text">2.2 软件模拟</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-%E8%B5%B7%E5%A7%8B%E4%BF%A1%E5%8F%B7%E6%A8%A1%E6%8B%9F"><span class="nav-text">2.2.1 起始信号模拟</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-%E5%81%9C%E6%AD%A2%E4%BF%A1%E5%8F%B7%E6%A8%A1%E6%8B%9F"><span class="nav-text">2.2.2 停止信号模拟</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%BA%94%E7%AD%94%E4%BF%A1%E5%8F%B7"><span class="nav-text"> 3. 应答信号</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E7%9B%B8%E5%85%B3%E6%A6%82%E5%BF%B5"><span class="nav-text">3.1 相关概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E8%BD%AF%E4%BB%B6%E6%A8%A1%E6%8B%9F"><span class="nav-text">3.2 软件模拟</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1-%E4%BA%A7%E7%94%9FACK%E5%BA%94%E7%AD%94%E4%BF%A1%E5%8F%B7"><span class="nav-text">3.2.1 产生ACK应答信号</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2-%E4%B8%8D%E4%BA%A7%E7%94%9FACK%E5%BA%94%E7%AD%94%E4%BF%A1%E5%8F%B7"><span class="nav-text">3.2.2 不产生ACK应答信号</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-3-%E7%AD%89%E5%BE%85%E5%BA%94%E7%AD%94%E4%BF%A1%E5%8F%B7"><span class="nav-text">3.2.3 等待应答信号</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%95%B0%E6%8D%AE%E4%BC%A0%E9%80%81"><span class="nav-text">4. 数据传送</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E7%9B%B8%E5%85%B3%E8%A7%84%E5%88%99"><span class="nav-text">4.1 相关规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E8%BD%AF%E4%BB%B6%E6%A8%A1%E6%8B%9F"><span class="nav-text">4.2 软件模拟</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-1-%E5%8F%91%E9%80%81%E4%B8%80%E4%B8%AA%E5%AD%97%E8%8A%82"><span class="nav-text">4.2.1 发送一个字节</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-2-2-%E8%AF%BB%E5%8F%96%E4%B8%80%E4%B8%AA%E5%AD%97%E8%8A%82"><span class="nav-text">4.2.2 读取一个字节</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E6%80%BB%E7%BA%BF%E4%BB%B2%E8%A3%81%E6%9C%BA%E5%88%B6"><span class="nav-text">5. 总线仲裁机制</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E5%9C%B0%E5%9D%80%E5%8F%8A%E6%95%B0%E6%8D%AE%E6%96%B9%E5%90%91"><span class="nav-text">6. 地址及数据方向</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81-I2C-%E8%AF%BB%E5%86%99%E6%97%B6%E5%BA%8F"><span class="nav-text">五、 I2C 读写时序</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-I2C-%E5%86%99%E6%97%B6%E5%BA%8F"><span class="nav-text">1. I2C 写时序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-I2C-%E8%AF%BB%E6%97%B6%E5%BA%8F"><span class="nav-text">2. I2C 读时序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-I2C-%E5%A4%8D%E5%90%88%E9%80%9A%E4%BF%A1"><span class="nav-text">3. I2C 复合通信</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%AE%8C%E6%95%B4%E4%BC%A0%E8%BE%93%E6%B5%81%E7%A8%8B"><span class="nav-text">4. 完整传输流程</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81AT24C02-%E5%AE%9E%E4%BE%8B"><span class="nav-text">六、AT24C02 实例</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%8D%95%E5%AD%97%E8%8A%82%E5%86%99%E6%97%B6%E5%BA%8F"><span class="nav-text">1. 单字节写时序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%8D%95%E5%AD%97%E8%8A%82%E8%AF%BB%E6%97%B6%E5%BA%8F"><span class="nav-text">2. 单字节读时序</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/1c55a2ac.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="通信协议-03-I2C通信 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          通信协议-03-I2C通信
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-08-30 17:31:13" itemprop="dateCreated datePublished" datetime="2022-08-30T17:31:13+08:00">2022-08-30</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/" itemprop="url" rel="index"><span itemprop="name">03通信协议</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>6.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>25 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是通信协议——I2C通信相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">驱动开发指南</td><td align="left"><a href="http://47.111.11.73/docs/boards/arm-linux/zdyz-i.mx6ull.html#i-mx6ull-linux" target="_blank">i.MX6ULL Linux阿尔法开发板资料 <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">STM32开发指南</td><td align="left"><a href="http://www.openedv.com/docs/boards/stm32/zdyz_stm32f103_warship.html#id3" target="_blank">STM32F1开发指南-库函数版本_V3.3 <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">野火STM32开发指南</td><td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103badao/std/zh/latest/book/communication.html" target="_blank">STM32库开发实战指南——基于野火霸道开发板 文档 (embedfire.com) <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、I2C简介"><a href="#一、I2C简介" class="headerlink" title="一、I2C简介"></a><font size=3>一、I2C简介</font></h1><h2 id="1-I2C介绍"><a href="#1-I2C介绍" class="headerlink" title="1. I2C介绍"></a><font size=3>1. I2C介绍</font></h2><p>I2C 就是 I2C （ Inter－Integrated Circuit ）总线，是一种多主从架构串行通信总线。在1980年由 PHILIPS  （飞利浦）公司开发的两线式<strong>串行</strong>、半双工总线，用于连接微控制器及其外围设备。它可以让主板、嵌入式系统或手机连接低速周边设备， 如今在嵌入式领域是非常常见。</p>
<p>它是由数据线  SDA  和时钟  SCL  构成的串行总线，可发送和接收数据。在  CPU  与被控  IC  之间、  IC  与  IC  之间进行双向传送数据， 标准 I2C 总线传输速率可到 100kbps ，高速  I2C  总线一般可达  400kbps  以上。</p>
<p>主要用于近距离、低速的芯片之间的通信； I2C 总线有两根双向的信号线一根数据线 SDA 用于收发数据，一根时钟线 SCL 用于通信双方时钟的同步； I2C 总线硬件结构简单，成本较低，因此在各个领域得到了广泛的应用。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903122402843.png" alt="image-20220903122402843" style="zoom: 50%;" />

<p>每个接到I2C总线上的器件都有一个唯一的地址，主机与其他器件进行数据传输的时候，在I2C总线上发送数据的称之为<strong>发送器</strong>，总线上接收数据的器件被称为<strong>接收器</strong>。</p>
<h2 id="2-模拟I2C与硬件I2C"><a href="#2-模拟I2C与硬件I2C" class="headerlink" title="2. 模拟I2C与硬件I2C"></a><font size=3>2. 模拟I2C与硬件I2C</font></h2><p>因为I2C协议比较简单，常常用GPIO来模拟I2C时序，这种方法称为模拟I2C。如果使用MCU的I2C控制器，设置好I2C控制器, I2C控制器就自动实现协议时序，这种方式称为硬件I2C。因为I2C设备的速率比较低，通常两种方式都可以，模拟I2C方便移植，硬件I2C工作效率相对较高。  </p>
<h2 id="3-I2C-总线特点"><a href="#3-I2C-总线特点" class="headerlink" title="3. I2C 总线特点"></a><font size=3>3. I2C 总线特点</font></h2><p>（1）I2C 总线是一种多主机总线，连接在 I2C 总线上的器件分为主机和从机两种，主机有权发起和结束一次通信，而从机只能被主机呼叫；当总线上有多个主机同时启用总线时， I2C 也具备<strong>冲突检测和仲裁</strong>的功能来决定由哪个设备占用总线，防止错误产生。</p>
<p>（2）每个连接到 I2C 总线上的器件都有一个唯一的地址( 7bit )，且每个器件都可以作为主机也可以作为从机（同一时刻只能有一个主机），总线上的器件增加和删除不影响其他器件正常工作； I2C 总线在通信时总线上发送数据的器件称为<strong>发送器</strong>，接收数据的器件称为<strong>接收器</strong>。</p>
<p>（3）总线通过上拉电阻接到电源。当 I2C 设备空闲时，会输出高阻态， 而当所有设备都空闲，都输出高阻态时，由上拉电阻把总线拉成高电平。</p>
<p>（4）具有三种传输模式：标准模式传输速率为 100kbit&#x2F;s  ，快速模式为 400kbit&#x2F;s  ， 高速模式下可达  3.4Mbit&#x2F;s ，但目前大多 I2C 设备尚不支持高速模式。</p>
<p>连接到相同总线的  IC  数量受到总线的最大电容  400pF  限制。</p>
<h1 id="二、基本原理？"><a href="#二、基本原理？" class="headerlink" title="二、基本原理？"></a><font size=3>二、基本原理？</font></h1><h2 id="1-一个实例"><a href="#1-一个实例" class="headerlink" title="1. 一个实例"></a><font size=3>1. 一个实例</font></h2><p>关于I²C协议，我在学习的时候在一个资料上看到了一个例子，大概就是老师（ MCU）将球（数据）传给众多学生中的一个（众多外设设备中的一个）。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20230502083209035.png" alt="image-20230502083209035" style="zoom:50%;" />

<ul>
<li>首先老师将球踢给某学生， 即主机发送数据给从机，步骤如下：</li>
</ul>
<p>（1）老师：开始了(start)；</p>
<p>（2）老师： A！我要发球给你！ (地址&#x2F;方向)；</p>
<p>（3）学生A：到！ (回应)；</p>
<p>（4）老师把球发出去（传输） ；</p>
<p>（5）A收到球之后，应该告诉老师一声（回应） ；</p>
<p>（6）老师：结束（停止） ；  </p>
<ul>
<li>接着老师让学生把球传给自己，即从机发送数据给主机，步骤如下：</li>
</ul>
<p>（1）老师：开始了(start)；</p>
<p>（2）老师： B！把球发给我！ (地址&#x2F;方向)；</p>
<p>（3） 学生B：到！</p>
<p>（4）B把球发给老师（传输） ；</p>
<p>（5）老师收到球之后，给B说一声，表示收到球了（回应） ；</p>
<p>（6）老师：结束（停止） 。  </p>
<h2 id="2-实例总结"><a href="#2-实例总结" class="headerlink" title="2. 实例总结"></a><font size=3>2. 实例总结</font></h2><p>从上面的例子可知， 都是老师（主机） 主导传球， 按照规范的流程（通信协议），以保证传球的准确性，收发球的流程总结如下：</p>
<p>（1）老师说开始了，表示开始信号(start)；</p>
<p>（2）老师提醒某个学生要发球，表示发送地址和方向(address&#x2F;read&#x2F;write)；</p>
<p>（3）该学生回应老师(ack)；</p>
<p>（4）老师发球&#x2F;接球，表示数据的传输；</p>
<p>（5）收到球要回应：回应信号(ACK)；</p>
<p>（6）老师说结束，表示I2C传输结束(P)。</p>
<p>以上就是I2C的传输协议，如果是软件模拟I2C，需要依次实现每个步骤。 因此，还需要知道每一步的具体细节，比如什么时候的数据有效， 开始信号怎么表示。  </p>
<h1 id="三、I2C-协议相关术语"><a href="#三、I2C-协议相关术语" class="headerlink" title="三、I2C 协议相关术语"></a><font size=3>三、I2C 协议相关术语</font></h1><p>这一部分会结合实例代码来说明，主要是通过模拟I2C来学习。</p>
<h2 id="1-相关宏定义"><a href="#1-相关宏定义" class="headerlink" title="1. 相关宏定义"></a><font size=3>1. 相关宏定义</font></h2><p>下边的软件模拟的平台为 STM32F103ZET6 ，所出现的宏定义有：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> SDA_IN()  &#123;GPIOB-&gt;CRL&amp;=0X0FFFFFFF;GPIOB-&gt;CRL|=(u32)8&lt;&lt;28;&#125; <span class="comment">/* 设置GPIO为输入 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SDA_OUT() &#123;GPIOB-&gt;CRL&amp;=0X0FFFFFFF;GPIOB-&gt;CRL|=(u32)3&lt;&lt;28;&#125; <span class="comment">/* 设置GPIO为输出 */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//IO操作函数	 </span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> IIC_SCL    PBout(6) <span class="comment">/* SCL时钟线 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> IIC_SDA    PBout(7) <span class="comment">/* SDA输出 */</span>	 </span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> READ_SDA   PBin(7)  <span class="comment">/* SDA输入 */</span> </span></span><br></pre></td></tr></table></figure>

<h2 id="2-空闲状态"><a href="#2-空闲状态" class="headerlink" title="2. 空闲状态"></a><font size=3>2. 空闲状态</font></h2><p>I2C 总线的 SDA 和 SCL 两条信号线<strong>同时处于高电平</strong>时，规定为总线的<strong>空闲状态</strong>。此时各个器件的输出级场效应管均处在截止状态（对外为高阻态），即释放总线，由两条信号线各自的上拉电阻把电平拉高。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903122435393.png" alt="image-20220903122435393" style="zoom:50%;" />

<h2 id="2-起始信号和停止信号"><a href="#2-起始信号和停止信号" class="headerlink" title="2. 起始信号和停止信号"></a><font size=3>2. 起始信号和停止信号</font></h2><h3 id="2-1相关概念"><a href="#2-1相关概念" class="headerlink" title="2.1相关概念"></a><font size=3>2.1相关概念</font></h3><ul>
<li><p>起始信号（S）：当 SCL 为高期间， SDA 由<strong>高到低的跳变</strong>；起始信号是一种电平跳变时序信号，而不是一个电平信号。</p>
</li>
<li><p>停止信号（P）：当 SCL 为高期间， SDA 由<strong>低到高的跳变</strong>；停止信号也是一种电平跳变时序信号，而不是一个电平信号。</p>
</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903122708365.png" alt="image-20220903122708365" style="zoom:50%;" />

<p><strong>【注意】</strong>起始信号和停止信号都是由主机发出，<strong>起始信号产生后总线处于占用状态</strong>，<strong>停止信号产生后总线处于空闲状态</strong>。</p>
<h3 id="2-2-软件模拟"><a href="#2-2-软件模拟" class="headerlink" title="2.2 软件模拟"></a><font size=3>2.2 软件模拟</font></h3><h4 id="2-2-1-起始信号模拟"><a href="#2-2-1-起始信号模拟" class="headerlink" title="2.2.1 起始信号模拟"></a><font size=3>2.2.1 起始信号模拟</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 产生IIC起始信号 */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">IIC_Start</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	SDA_OUT();     <span class="comment">/* SDA 数据线设置为输出 */</span></span><br><span class="line">	IIC_SDA=<span class="number">1</span>;	   <span class="comment">/* SDA 数据线拉高表示空闲状态 */</span>  </span><br><span class="line">	IIC_SCL=<span class="number">1</span>;     <span class="comment">/* SCL 时钟线拉高表示空闲状态 */</span>  </span><br><span class="line">	delay_us(<span class="number">4</span>);   </span><br><span class="line"> 	IIC_SDA=<span class="number">0</span>;     <span class="comment">/* START:when CLK is high, DATA change form high to low  */</span></span><br><span class="line">	delay_us(<span class="number">4</span>);</span><br><span class="line">	IIC_SCL=<span class="number">0</span>;     <span class="comment">/* 钳住I2C总线(拉低SCL时钟线，SDA数据线也为低)，准备发送或接收数据  */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-2-2-停止信号模拟"><a href="#2-2-2-停止信号模拟" class="headerlink" title="2.2.2 停止信号模拟"></a><font size=3>2.2.2 停止信号模拟</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 产生IIC停止信号 */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">IIC_Stop</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	SDA_OUT();   <span class="comment">/* SDA 数据线设置为输出 */</span></span><br><span class="line">	IIC_SCL=<span class="number">0</span>;   <span class="comment">/* SCL 时钟线拉低 */</span>  </span><br><span class="line">	IIC_SDA=<span class="number">0</span>;   <span class="comment">/* STOP: when CLK is high，DATA change form low to high (为停止信号做准备)*/</span></span><br><span class="line"> 	delay_us(<span class="number">4</span>);</span><br><span class="line">	IIC_SCL=<span class="number">1</span>;   <span class="comment">/* SCL 时钟线拉高，并保持高电平 */</span>  </span><br><span class="line">	IIC_SDA=<span class="number">1</span>;   <span class="comment">/* SDA 数据线原来为低，现在拉高，产生一个由低到高的跳变，表示I2C总线停止信号 */</span></span><br><span class="line">	delay_us(<span class="number">4</span>);							   	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-应答信号"><a href="#3-应答信号" class="headerlink" title=" 3. 应答信号"></a><font size=3> 3. 应答信号</font></h2><h3 id="3-1-相关概念"><a href="#3-1-相关概念" class="headerlink" title="3.1 相关概念"></a><font size=3>3.1 相关概念</font></h3><p>发送器每发送一个字节（8位数据），每次传输后由<strong>接收器（从机）反馈一个应答信号</strong>，以<strong>确认从机是否正常接收了数据</strong>。 当主机发送了8位数据后，会再产生一个时钟，  也就是在时钟的<strong>第 9 个脉冲</strong>（前 8 个表示传输 8 位数据）期间释放 SDA 数据线，主机由输出转为读取，读取SDA电平， 这个时候SDA数据线在上拉电阻的影响下，此时SDA默认为高，必须从机拉低， 以确认收到数据。   </p>
<ul>
<li>应答信号为低电平时，规定为<strong>有效应答位</strong>（ ACK 简称应答位），表示接收器已经成功地接收了该字节。</li>
<li>应答信号为高电平时，规定为<strong>非应答位</strong>（ NACK ），一般表示接收器接收该字节没有成功，非应答位可以用于告诉主机结束数据发送。</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903124720178.png" alt="image-20220903124720178" style="zoom:50%;" />

<p>当  I2C  主机发送完  8 位数据以后会释放SDA总线，将  SDA  设置为输入状态，等待  I2C  从机应答，也就是等到  I2C  从机告诉主机它接收到数据了。<strong>应答信号是由从机发出的</strong>，主机需要提供应答信号所需的时钟，主机发送完  8  位数据以后紧跟着的一个时钟信号就是给应答信号使用的。从机通过将  SDA  拉低来表示发出应答信号，表示通信成功，否则表示通信失败。  </p>
<p><strong>【注意】</strong></p>
<p>（1）为了传输一个ACK位，主机端需要产生一个时钟脉冲，也就是第九个时钟脉冲；</p>
<p>（2）对于反馈有效应答位 ACK 的要求是，接收器在第 9 个时钟脉冲之前的低电平期间将 SDA 线拉低，并且确保在该时钟的高电平期间SDA为稳定的低电平。 如果接收器是主控器，则在它收到最后一个字节后，发送一个 NACK 信号（即不发出ACK信号），以通知被控发送器结束数据发送，并释放 SDA 线，以便主控接收器发送一个停止信号 P 。</p>
<h3 id="3-2-软件模拟"><a href="#3-2-软件模拟" class="headerlink" title="3.2 软件模拟"></a><font size=3>3.2 软件模拟</font></h3><h4 id="3-2-1-产生ACK应答信号"><a href="#3-2-1-产生ACK应答信号" class="headerlink" title="3.2.1 产生ACK应答信号"></a><font size=3>3.2.1 产生ACK应答信号</font></h4><ul>
<li>从机应答信号模拟（这种情况一般是从从机读数据，主机产生应答，告诉从机我读完了）</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 产生ACK应答 */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">IIC_Ack</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	IIC_SCL=<span class="number">0</span>;    <span class="comment">/* SCL 时钟线拉低 */</span></span><br><span class="line">	SDA_OUT();    <span class="comment">/* SDA 数据线设置为输出 */</span></span><br><span class="line">	IIC_SDA=<span class="number">0</span>;    <span class="comment">/* SDA 数据线拉低（应答，第9个脉冲期间保持低电平） */</span></span><br><span class="line">	delay_us(<span class="number">2</span>); </span><br><span class="line">	<span class="comment">/* SCL 时钟线产生一个脉冲（第9个，低-&gt;高-&gt;低） */</span></span><br><span class="line">	IIC_SCL=<span class="number">1</span>;    <span class="comment">/* SCL 时钟线拉高 */</span></span><br><span class="line">	delay_us(<span class="number">2</span>);</span><br><span class="line">	IIC_SCL=<span class="number">0</span>;    <span class="comment">/* SCL 时钟线拉低 */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-2-2-不产生ACK应答信号"><a href="#3-2-2-不产生ACK应答信号" class="headerlink" title="3.2.2 不产生ACK应答信号"></a><font size=3>3.2.2 不产生ACK应答信号</font></h4><ul>
<li>从机非应答信号模拟（这种情况一般是从从机读数据，主机不产生应答，这时候要注意接收应答的一方若是一直等待的话可能就会有问题了）</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 不产生ACK应答 */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">IIC_NAck</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	IIC_SCL=<span class="number">0</span>;   <span class="comment">/* SCL 时钟线拉低 */</span></span><br><span class="line">	SDA_OUT();   <span class="comment">/* SDA 数据线设置为输出 */</span></span><br><span class="line">	IIC_SDA=<span class="number">1</span>;   <span class="comment">/* SDA 数据线拉高（不应答，第9个脉冲期间保持高电平） */</span></span><br><span class="line">	delay_us(<span class="number">2</span>);</span><br><span class="line">	<span class="comment">/* SCL 时钟线产生一个脉冲（第9个）（第9个，低-&gt;高-&gt;低） */</span></span><br><span class="line">	IIC_SCL=<span class="number">1</span>;   <span class="comment">/* SCL 时钟线拉高 */</span></span><br><span class="line">	delay_us(<span class="number">2</span>);</span><br><span class="line">	IIC_SCL=<span class="number">0</span>;   <span class="comment">/* SCL 时钟线拉低 */</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-2-3-等待应答信号"><a href="#3-2-3-等待应答信号" class="headerlink" title="3.2.3 等待应答信号"></a><font size=3>3.2.3 等待应答信号</font></h4><ul>
<li>主机等待应答信号（这种情况一般是向从机写数据，从机会产生应答，从机的应答不需要我们来管，读到应答信号的时候表示写入完成，从机收到了数据）</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">unsigned</span> <span class="type">char</span> <span class="title function_">IIC_Wait_Ack</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">unsigned</span> <span class="type">char</span> ucErrTime=<span class="number">0</span>;</span><br><span class="line">	SDA_IN();      <span class="comment">// SDA设置为输入  </span></span><br><span class="line">	IIC_SDA=<span class="number">1</span>;delay_us(<span class="number">1</span>);	   </span><br><span class="line">	IIC_SCL=<span class="number">1</span>;delay_us(<span class="number">1</span>);	 </span><br><span class="line">	<span class="keyword">while</span>(READ_SDA)</span><br><span class="line">	&#123;</span><br><span class="line">		ucErrTime++;</span><br><span class="line">		<span class="keyword">if</span>(ucErrTime&gt;<span class="number">250</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			IIC_Stop();</span><br><span class="line">			<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	IIC_SCL=<span class="number">0</span>; <span class="comment">// 时钟输出0 	   </span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;  </span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>



<h2 id="4-数据传送"><a href="#4-数据传送" class="headerlink" title="4. 数据传送"></a><font size=3>4. 数据传送</font></h2><h3 id="4-1-相关规则"><a href="#4-1-相关规则" class="headerlink" title="4.1 相关规则"></a><font size=3>4.1 相关规则</font></h3><p>I2C 总线进行数据传送时</p>
<ul>
<li><p>时钟线 SCL 上的信号为高电平期间：接收器从数据线上读取一位数据，数据线 SDA 上的数据必须保持稳定，不允许发生变化。也就是， SCL为高电平时表示有效数据， SDA为高电平表示“ 1”，低电平表示“0”。</p>
</li>
<li><p>时钟线 SCL 上的信号为低电平期间：数据线上的高电平或低电平状态允许变化，只有在这种情况下发送器才能向数据线上发送一位数据。也就是 SCL为低电平时表示无效数据，此时SDA可以进行电平切换，为下次数据表示做准备。</p>
</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903124206035.png" alt="image-20220903124206035" style="zoom:50%;" />

<p>在 I2C 总线上传送的每一位数据都有一个时钟脉冲相对应（或同步控制），即在 SCL 串行时钟的配合下，在 SDA 上<strong>逐位</strong>地串行传送每一位数据，数据位的传输是边沿触发。</p>
<p>I2C 总线通信时每个字节为 8 位长度，数据传送时，<strong>先传送最高位，后传送低位</strong>，发送器发送完一个字节数据后接收器必须发送 1 位应答位来回应发送器即一帧共有 9 位。</p>
<p>【注意】数据在 SCL 的上升沿到来之前就需准备好。并在在下降沿到来之前必须稳定。</p>
<h3 id="4-2-软件模拟"><a href="#4-2-软件模拟" class="headerlink" title="4.2 软件模拟"></a><font size=3>4.2 软件模拟</font></h3><h4 id="4-2-1-发送一个字节"><a href="#4-2-1-发送一个字节" class="headerlink" title="4.2.1 发送一个字节"></a><font size=3>4.2.1 发送一个字节</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 发送1个字节，txd表示要发送的一个字节数据 */</span>		  </span><br><span class="line"><span class="type">void</span> <span class="title function_">IIC_Send_Byte</span><span class="params">(<span class="type">unsigned</span> <span class="type">char</span> txd)</span></span><br><span class="line">&#123;                        </span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> t;   </span><br><span class="line">	SDA_OUT(); 	    </span><br><span class="line">    IIC_SCL=<span class="number">0</span>;             <span class="comment">/* 拉低 SCL 时钟线，准备开始数据传输（SCL为低时 SDA上的信号才允许变化） */</span></span><br><span class="line">    <span class="keyword">for</span>(t=<span class="number">0</span>;t&lt;<span class="number">8</span>;t++)       <span class="comment">/* 循环发送8位数据 */</span></span><br><span class="line">    &#123;              </span><br><span class="line">        <span class="comment">//IIC_SDA=(txd&amp;0x80)&gt;&gt;7;</span></span><br><span class="line">		<span class="keyword">if</span>((txd&amp;<span class="number">0x80</span>)&gt;&gt;<span class="number">7</span>)  <span class="comment">/* 获取最高位（0x80=1000 0000），将最高位右移7位到最低位。再判断这一位0还是为1 */</span></span><br><span class="line">			IIC_SDA=<span class="number">1</span>;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">			IIC_SDA=<span class="number">0</span>;</span><br><span class="line">		txd&lt;&lt;=<span class="number">1</span>; 	        <span class="comment">/* 将下一位左移到最高位，为下一次发送做准备 */</span>  </span><br><span class="line">		delay_us(<span class="number">2</span>);</span><br><span class="line">		<span class="comment">/* 数据准备完毕，接下来SCL产生一个 低-&gt;高-&gt;低 的脉冲变化，完成一位数据发送 */</span></span><br><span class="line">		IIC_SCL=<span class="number">1</span>;          <span class="comment">/* SCL 时钟线拉高 */</span></span><br><span class="line">		delay_us(<span class="number">2</span>); </span><br><span class="line">		IIC_SCL=<span class="number">0</span>;	        <span class="comment">/* SCL 时钟线拉低 */</span></span><br><span class="line">		delay_us(<span class="number">2</span>);</span><br><span class="line">    &#125;	 </span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<h4 id="4-2-2-读取一个字节"><a href="#4-2-2-读取一个字节" class="headerlink" title="4.2.2 读取一个字节"></a><font size=3>4.2.2 读取一个字节</font></h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 读1个字节，ack=1时，发送ACK，ack=0，发送nACK */</span>   </span><br><span class="line"><span class="type">unsigned</span> <span class="type">char</span> <span class="title function_">IIC_Read_Byte</span><span class="params">(<span class="type">unsigned</span> <span class="type">char</span> ack)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">unsigned</span> <span class="type">char</span> i,receive=<span class="number">0</span>;</span><br><span class="line">	SDA_IN();              <span class="comment">/* SDA 数据线设置为输入 */</span></span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">8</span>;i++ )      <span class="comment">/* 循环读取8位数据 */</span></span><br><span class="line">	&#123;</span><br><span class="line">        IIC_SCL=<span class="number">0</span>;         <span class="comment">/* SCL 时钟线拉低 */</span></span><br><span class="line">        delay_us(<span class="number">2</span>);</span><br><span class="line">		IIC_SCL=<span class="number">1</span>;         <span class="comment">/* SCL 时钟线拉高（高电平期间从数据线SDA读取1位数据） */</span></span><br><span class="line">        receive&lt;&lt;=<span class="number">1</span>;       <span class="comment">/* 数据左移，因为发送方先发的是高位 */</span></span><br><span class="line">        <span class="keyword">if</span>(READ_SDA) receive++;   <span class="comment">/* 读到的是1的话，最低位加1 */</span></span><br><span class="line">		delay_us(<span class="number">1</span>); </span><br><span class="line">    &#125;					 </span><br><span class="line">    <span class="keyword">if</span> (!ack)</span><br><span class="line">        IIC_NAck();        <span class="comment">/* 发送nACK（不应答） */</span></span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        IIC_Ack();         <span class="comment">/* 发送ACK（应答） */</span>   </span><br><span class="line">    <span class="keyword">return</span> receive;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5-总线仲裁机制"><a href="#5-总线仲裁机制" class="headerlink" title="5. 总线仲裁机制"></a><font size=3>5. 总线仲裁机制</font></h2><p>总线上可能挂接有多个器件，有时会发生两个或多个主器件同时想占用总线的情况，这种情况叫做总线竞争。I2C总线具有多主控能力，可以对发生在SDA线上的总线竞争进行仲裁，其仲裁原则是：<strong>当多个主器件同时想占用总线时，如果某个主器件发送高电平，而另一个主器件发送低电平，则发送电平与此时SDA总线电平不符的那个器件将自动关闭其输出级</strong>。</p>
<p>总线竞争的仲裁是在两个层次上进行的。首先是地址位的比较，如果主器件寻址同一个从器件，则进入数据位的比较，从而确保了竞争仲裁的可靠性。由于是利用I2C总线上的信息进行仲裁，因此不会造成信息的丢失。</p>
<h2 id="6-地址及数据方向"><a href="#6-地址及数据方向" class="headerlink" title="6. 地址及数据方向"></a><font size=3>6. 地址及数据方向</font></h2><p>I2C总线上的每个设备都有自己的独立地址，主机发起通讯时，通过SDA信号线发送设备地址(SLAVE_ADDRESS)来查找从机。设备地址可以是7位或10位。  紧跟设备地址的一个数据位R&#x2F;W用来表示数据传输方向， 数据方向位为“1”时表示主机由从机读数据，该位为“0”时表示主机向从机写数据。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20230507104655753.png" alt="image-20230507104655753" style="zoom:50%;" />



<h1 id="五、-I2C-读写时序"><a href="#五、-I2C-读写时序" class="headerlink" title="五、 I2C 读写时序"></a><font size=3>五、 I2C 读写时序</font></h1><p>从机接收到匹配的地址后，主机或从机会返回一个应答( ACK )或非应答( NACK )信号，只有接收到应答信号后，主机才能继续发送或接收数据。</p>
<h2 id="1-I2C-写时序"><a href="#1-I2C-写时序" class="headerlink" title="1. I2C 写时序"></a><font size=3>1. I2C 写时序</font></h2><p>主机写数据到从机的时序如下图所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903151323709.png" alt="image-20220903151323709" style="zoom:50%;" />

<ul>
<li>（1）主机发送一个起始信号S，表示启动I2C；</li>
<li>（2）发送从机 7 位地址和 1 位读写标志，读写标志为0，表示写；</li>
<li>（3）主机释放 SDA 数据线（拉高），便于后续接收从机的回应，若有从机匹配到主机发送的地址，则从机会拉低 SDA 线作为ACK（有效应答）；</li>
<li>（4）接收到应答后，主机重新拉低 SDA，准备发送数据，然后开始传输8位数据，先发高位，再发低位；</li>
<li>（5）8 位数据传输完成后，主机释放SDA线，便于接收从机发出的应答或者非应答信号；</li>
<li>（6）从机收到数据后，拉低 SDA数据线作为 ACK （应答信号），告诉主机数据接收成功；</li>
<li>（7）主机发出停止信号，表示结束I2C通信。</li>
</ul>
<h2 id="2-I2C-读时序"><a href="#2-I2C-读时序" class="headerlink" title="2. I2C 读时序"></a><font size=3>2. I2C 读时序</font></h2><p>主机由从机中读取数据（从机写数据到主机）的时序如下图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903151831570.png" alt="image-20220903151831570" style="zoom:50%;" />

<ul>
<li>（1）主机发送一个起始信号S，表示启动I2C；</li>
<li>（2）发送从机7位地址和1位读写标志，读写标志为1表示读；</li>
<li>（3）主机释放 SDA 数据线便于后续接收从机的回应，当有从机匹配到地址时，从机会拉低SDA数据线作为 ACK（有效应答）；</li>
<li>（4）然后从机继续拉高SDA线，准备开始使用SDA数据线传输 8 位数据给主机；</li>
<li>（5）8 位数据传输完毕后，从机释放SDA数据线(拉高)，便于接收主机的回应；</li>
<li>（6）主机接收到数据；</li>
<li>（7）主机获得SDA线控制权限，并拉低SDA数据线作为 ACK （有效应答）告诉从机数据接收成功；</li>
<li>（8）主机发出停止信号，表示结束I2C。</li>
</ul>
<h2 id="3-I2C-复合通信"><a href="#3-I2C-复合通信" class="headerlink" title="3. I2C 复合通信"></a><font size=3>3. I2C 复合通信</font></h2><p>我们操作 I2C 设备的时候，多数情况都是先向设备写入数据，后续就是从设备中读取数据了，主机先向从机发送数据，然后从机再向主机发送数据的时序如下图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903153506965.png" alt="image-20220903153506965" style="zoom:50%;" />

<h2 id="4-完整传输流程"><a href="#4-完整传输流程" class="headerlink" title="4. 完整传输流程"></a><font size=3>4. 完整传输流程</font></h2><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20230502090201529.png" alt="image-20230502090201529" style="zoom:80%;" />

<p>I2C完整传输流程如下：</p>
<p>（1）SDA和SCL开始都为高， 然后主机将SDA拉低， 表示开始信号；</p>
<p>（2）在接下来的8个时间周期里，主机控制SDA的高低， 发送从机地址。 其中第8位如果为0， 表示接下来是写操作，即主机传输数据给从机； 如果为1，表示接下来是读操作，即从机传输数据给主机； 另外， 数据传输是从最高位到最低位，因此传输方式为MSB（ Most Significant Bit）。</p>
<p>（3）总线中对应从机地址的设备，发出应答信号；</p>
<p>（4）在接下来的8个时间周期里，如果是写操作，则主机控制SDA的高低；如果是读操作，则从机控制SDA的高低；</p>
<p>（5）每次传输完成， 接收数据的设备， 都发出应答信号；</p>
<p>（6）最后， 在SCL为高时， 主机由低拉高SDA， 表示停止信号，整个传输结束；  </p>
<h1 id="六、AT24C02-实例"><a href="#六、AT24C02-实例" class="headerlink" title="六、AT24C02 实例"></a><font size=3>六、AT24C02 实例</font></h1><p>AT24C02 是一个 256 （ 2K&#x2F;8 )个字节的 EEPROM ，它使用的接口就是 I2C ，在它的芯片手册中，给出了读写时序的时序图，我们就以此器件为例分析一下 I2C 的读写时序。</p>
<h2 id="1-单字节写时序"><a href="#1-单字节写时序" class="headerlink" title="1. 单字节写时序"></a><font size=3>1. 单字节写时序</font></h2><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903160151362.png" alt="image-20220903160151362" style="zoom:50%;" />

<p>（1）开始信号。</p>
<p>（2）发送  I2C  设备地址，每个  I2C  器件都有一个设备地址，通过发送具体的设备地址来决定访问哪个  I2C  器件。这是一个  8  位的数据，其中高  7  位是设备地址，最后  1  位是读写位，为 1  的话表示这是一个读操作，为  0  的话表示这是一个写操作。</p>
<p>（3）  I2C  器件地址后面跟着一个读写位，为  0  表示写操作，为  1  表示读操作。</p>
<p>（4）从机发送的  ACK  应答信号。</p>
<p>（5）重新发送开始信号。</p>
<p>（6）发送要写写入数据的寄存器地址。</p>
<p>（7）从机发送的  ACK  应答信号。</p>
<p>（8）发送要写入寄存器的数据。  </p>
<p>（9）从机发送的  ACK  应答信号。</p>
<p>（10）停止信号。</p>
<h2 id="2-单字节读时序"><a href="#2-单字节读时序" class="headerlink" title="2. 单字节读时序"></a><font size=3>2. 单字节读时序</font></h2><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/03%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE-03-I2C%E9%80%9A%E4%BF%A1/img/image-20220903162111744.png" alt="image-20220903162111744" style="zoom:50%;" />

<p>I2C  单字节读时序比写时序要复杂一点，读时序分为  4  大步，第一步是发送设备地址，第二步是发送要读取的寄存器地址，第三步重新发送设备地址，最后一步就是  I2C  从器件输出要读取的寄存器值。</p>
<p>（1）主机发送起始信号。</p>
<p>（2）主机发送要读取的  I2C  从设备地址。</p>
<p>（3）读写控制位，因为是向  I2C  从设备发送数据，因此是写信号。</p>
<p>（4）从机发送的  ACK  应答信号。</p>
<p>（5）重新发送  START  信号。</p>
<p>（6）主机发送要读取的寄存器地址。</p>
<p>（7）从机发送的  ACK  应答信号。</p>
<p>（8）重新发送  START  信号。</p>
<p>（9）重新发送要读取的  I2C  从设备地址。</p>
<p>（10）读写控制位，这里是读信号，表示接下来是从  I2C  从设备里面读取数据。</p>
<p>（11）从机发送的  ACK  应答信号。</p>
<p>（12）从  I2C  器件里面读取到的数据。</p>
<p>（13）主机发出  NACK  信号，表示读取完成，不需要从机再发送  ACK  信号了。</p>
<p>（14）主机发出  STOP  信号，停止  I2C  通信。  </p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/1c55a2ac.html">通信协议-03-I2C通信</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年08月30日 - 17:31</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/1c55a2ac.html" title="通信协议-03-I2C通信">https://sumumm.github.io/post/1c55a2ac.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE/" rel="tag"><i class="fa fa-tag"></i> 通信协议</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/9d162041.html" rel="prev" title="LV01-10-C语言-函数传参问题">
                  <i class="fa fa-angle-left"></i> LV01-10-C语言-函数传参问题
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/22871ce2.html" rel="next" title="通信协议-02-串口通信">
                  通信协议-02-串口通信 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
